Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The c12 npm package is a configuration loader and manager that helps in loading, merging, and managing configurations from various sources such as files, environment variables, and more. It is designed to be flexible and easy to use, making it suitable for a wide range of applications.
Loading Configuration from Files
This feature allows you to load configuration from files. The `loadConfig` function reads configuration files based on the provided options such as the name of the application and the current working directory.
const { loadConfig } = require('c12');
async function loadConfigExample() {
const config = await loadConfig({
name: 'myapp',
cwd: process.cwd(),
rcFile: true
});
console.log(config);
}
loadConfigExample();
Merging Configurations
This feature allows you to merge configurations from different sources. The `loadConfig` function can take default configurations and override configurations, merging them into a single configuration object.
const { loadConfig } = require('c12');
async function mergeConfigExample() {
const config = await loadConfig({
name: 'myapp',
defaults: { key1: 'default1' },
overrides: { key2: 'override2' }
});
console.log(config);
}
mergeConfigExample();
Loading Configuration from Environment Variables
This feature allows you to load configuration from environment variables. The `loadConfig` function can be configured to read environment variables with a specific prefix and include them in the configuration object.
const { loadConfig } = require('c12');
async function envConfigExample() {
process.env.MYAPP_KEY = 'valueFromEnv';
const config = await loadConfig({
name: 'myapp',
envPrefix: 'MYAPP_'
});
console.log(config);
}
envConfigExample();
The 'config' package is a popular configuration management tool for Node.js applications. It allows you to define configurations for different environments and load them based on the current environment. Compared to c12, 'config' is more opinionated and structured, making it easier to manage complex configurations but less flexible in terms of merging and loading from multiple sources.
The 'dotenv' package loads environment variables from a .env file into process.env. It is simpler and more focused compared to c12, which offers more comprehensive configuration management capabilities including merging configurations from multiple sources.
The 'rc' package is a non-opinionated configuration loader that reads configuration from various sources like files, environment variables, and command-line arguments. It is similar to c12 in terms of flexibility but lacks some of the advanced features like merging configurations and handling defaults and overrides.
Smart Configuration Loader
.env
support with dotenvInstall package:
# npm
npm install c12
# yarn
yarn add c12
# pnpm
pnpm install c12
Import:
// ESM
import { loadConfig } from 'c12'
// CommonJS
const { loadConfig } = require('c12')
Load configuration:
// Get loaded config
const { config } = await loadConfig({})
// Get resolved config and extended layers
const { config, configFile, layers } = await loadConfig({})
c12 merged config sources with unjs/defu by below order:
cwd
Resolve configuration from this working directory. Default is process.cwd()
name
Configuration base name. Default is config
.
configName
Configuration file name without extension . Default is generated from name
(name=foo => foo.config
).
Set to false
to avoid loading config file.
rcFile
RC Config file name. Default is generated from name
(name=foo => .foorc
).
Set to false
to disable loading RC config.
globalRC
Load RC config from the workspace directory and user's home directory. Only enabled when rcFile
is provided. Set to false
to disable this functionality.
dotenv
Loads .env
file if enabled. It is disabled by default.
defaults
Specify default configuration. It has the lowest priority and is applied after extending config.
defaultConfig
Specify default configuration. It is applied before extending config.
overides
Specify override configuration. It has the highest priority and is applied before extending config.
jiti
Custom unjs/jiti instance used to import configuration files.
jitiOptions
Custom unjs/jiti options to import configuration files.
If resolved config contains a extends
key, it will be used to extend configuration.
Extending can be nested and each layer can extend from one base or more.
Final config is merged result of extended options and user options with unjs/defu.
Each item in extends, is a string that can be either an absolute or relative path to current config file pointing to a config file for extending or directory containing config file.
If it starts with either of github:
, gitlab:
, bitbucket:
or https:
, c12 autmatically clones it.
For custom merging strategies, you can directly access each layer with layers
property.
Example:
// config.ts
export default {
colors: {
primary: 'user_primary'
},
extends: [
'./theme',
'./config.dev.ts'
]
}
// config.dev.ts
export default {
dev: true
}
// theme/config.ts
export default {
extends: '../base',
colors: {
primary: 'theme_primary',
secondary: 'theme_secondary'
}
}
// base/config.ts
export default {
colors: {
primary: 'base_primary'
text: 'base_text'
}
}
Loaded configuration would look like this:
{
dev: true,
colors: {
primary: 'user_primary',
secondary: 'theme_secondary',
text: 'base_text'
}
}
Layers:
[
{ config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },
{ config: /* base config */, configFile: /* path/to/base/config.ts */, cwd: /* path/to/base */ },
{ config: /* dev config */, configFile: /* path/to/config.dev.ts */, cwd: /* path/ */ },
]
corepack enable
(use npm i -g corepack
for Node.js < 16.10)pnpm install
pnpm dev
Made with 💛 Published under MIT License.
FAQs
Smart Config Loader
The npm package c12 receives a total of 675,284 weekly downloads. As such, c12 popularity was classified as popular.
We found that c12 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.